package com.ktwlrj.core.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * list过滤方法
 *
 * @author zhou
 */
public class ListFilterUtils<A, B> {
    /**
     * 过滤判断
     */
    private Filter filter;

    /**
     * 原始listA
     */
    private List<A> originA;

    /**
     * 原始listB
     */
    private List<B> originB;

    /**
     * 相同部分B类型list
     */
    private List<A> commonA;

    /**
     * 相同部分A类型list
     */
    private List<B> commonB;

    public ListFilterUtils(Filter filter) {
        this.filter = filter;
    }

    public List<A> getOriginA() {
        return this.originA;
    }

    public List<B> getOriginB() {
        return this.originB;
    }

    public List<A> getCommonA() {
        return this.commonA;
    }

    public List<B> getCommonB() {
        return this.commonB;
    }

    /**
     * 删除两个集合相同的数据，同时返回原始listA
     *
     * @param listA ${@link List<A>}
     * @param listB ${@link List<B>}
     * @return List<A> ${@link List<A>} 返回listA原始数据
     * @author zhou
     * @date 2019-12-05 10:47
     */
    public void removeCommon(List<A> listA, List<B> listB) {
        List<A> originA = new ArrayList<>();
        List<B> originB = new ArrayList<>();
        List<A> commonA = new ArrayList<>();
        List<B> commonB = new ArrayList<>();
        Iterator<A> iteratorA = listA.iterator();
        while (iteratorA.hasNext()) {
            A a = iteratorA.next();
            Iterator<B> iteratorB = listB.iterator();
            while (iteratorB.hasNext()) {
                B b = iteratorB.next();
                if (filter.filterMethod(a, b)) {
                    commonA.add(a);
                    commonB.add(b);
                    iteratorB.remove();
                    iteratorA.remove();
                    break;
                }
            }
        }
        this.commonA = commonA;
        this.commonB = commonB;
        originA.addAll(commonA);
        originA.addAll(listA);
        originB.addAll(commonB);
        originB.addAll(listB);
    }


    /**
     * 过滤
     *
     * @author zhou
     * @date 2019-12-05 10:48
     * @since JDK1.8
     */
    public interface Filter<A, B> {
        /**
         * 过滤条件
         *
         * @param a ${@link A}
         * @param b ${@link B}
         * @return Boolean ${@link Boolean} false-不相同，true-相同
         * @author zhou
         * @date 2019-12-05 10:49
         */
        boolean filterMethod(A a, B b);
    }
}

